package com.gonsin.gview.model.dynamic.data;

import com.gonsin.gview.annotations.impl.EmptyListDataPicker;
import com.gonsin.gview.annotations.interfaces.IListDataPicker;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public class DynamicListData {

    /**
     * 直接输入sql语句查询数据（如果查询到多个，则会返回第一个）
     */
    private String fromSql = "";
    public static DynamicListDataBuilder fromSql(String fromSql){
        DynamicListDataBuilder data = new DynamicListDataBuilder();
        data.fromSql = fromSql;
        return data;
    }

    /**
     * 通过dataPicker写java语句查询数据（此方法最为灵活）
     */
    private IListDataPicker fromListDataPicker = null;
    public static DynamicListDataBuilder fromListDataPicker(IListDataPicker fromListDataPicker){
        DynamicListDataBuilder data = new DynamicListDataBuilder();
        data.fromListDataPicker = fromListDataPicker;
        return data;
    }

    /**
     * 通过dataPicker写java语句查询数据（此方法最为灵活）
     */
    private String fromListDataPickerClassName = "";
    public static DynamicListDataBuilder fromListDataPickerClassName(String fromListDataPickerClassName){
        DynamicListDataBuilder data = new DynamicListDataBuilder();
        data.fromListDataPickerClassName = fromListDataPickerClassName;
        return data;
    }

    /**
     * 通过静态json返回，适合静态数据
     */
    private String fromJson = "";
    public static DynamicListDataBuilder fromJson(String fromJson){
        DynamicListDataBuilder data = new DynamicListDataBuilder();
        data.fromJson = fromJson;
        return data;
    }

    /**
     * 从枚举名字获取列表
     */
    private String fromEnums = "";
    public static DynamicListData fromEnums(String fromEnums){
        DynamicListData data = new DynamicListData();
        data.fromEnums = fromEnums;
        return data;
    }

    /**
     * 通过@GDataSource获取数据
     */
    private String fromDataSource = "";
    public static DynamicListDataBuilder fromDataSource(String fromDataSource){
        DynamicListDataBuilder data = new DynamicListDataBuilder();
        data.fromDataSource = fromDataSource;
        return data;
    }

    /**
     * keyName对应列表中的主键
     */
    private String keyName = "key";

    /**
     * valueName对应列表中提交请求是填的值
     */
    private String valueName = "value";

    /**
     * titleName对应显示的名字
     */
    private String titleName = "";


    public static final class DynamicListDataBuilder {
        private String fromSql;
        private IListDataPicker fromListDataPicker;
        private String fromListDataPickerClassName;
        private String fromJson;
        private String fromEnums;
        private String fromDataSource;
        private String keyName;
        private String valueName;
        private String titleName;

        private DynamicListDataBuilder() {
        }
        public DynamicListDataBuilder keyName(String keyName) {
            this.keyName = keyName;
            return this;
        }

        public DynamicListDataBuilder valueName(String valueName) {
            this.valueName = valueName;
            return this;
        }

        public DynamicListDataBuilder titleName(String titleName) {
            this.titleName = titleName;
            return this;
        }

        public DynamicListData build() {
            DynamicListData dynamicListData = new DynamicListData();
            dynamicListData.setFromSql(fromSql);
            dynamicListData.setFromListDataPicker(fromListDataPicker);
            dynamicListData.setFromListDataPickerClassName(fromListDataPickerClassName);
            dynamicListData.setFromJson(fromJson);
            dynamicListData.setFromEnums(fromEnums);
            dynamicListData.setFromDataSource(fromDataSource);
            dynamicListData.setKeyName(keyName);
            dynamicListData.setValueName(valueName);
            dynamicListData.setTitleName(titleName);
            return dynamicListData;
        }
    }
}
